﻿using System.Web;
using System;
using System.Data;
using JKF.BusinessLayer.BLL;
using JKF.Substructure;

namespace JKF.Web
{
    public class PageBase : StructPageBase
    {
        /// <summary>
        /// 模块名称,在aspx.cs文件中进行模块名赋值，可以将默认值设置成文件所在文件夹名称，约定文件夹以模块名命名，就不用再每也页面赋值
        /// </summary>
        public string MODULE_NAME_EN = "default";

        /// <summary>
        /// 是否有模块的浏览权限
        /// </summary>
        protected bool IsView
        {
            get
            {
                bool result = CheckAction("浏览");
                if (result == false)
                    HttpContext.Current.Response.Redirect("~/noright.htm", true);

                return result;
            }
        }

        /// <summary>
        /// 是否有模块的添加权限
        /// </summary>
        protected bool IsAdd
        {
            get { return CheckAction("添加"); }
        }

        /// <summary>
        /// 是否有模块的编辑权限
        /// </summary>
        protected bool IsEdit
        {
            get { return CheckAction("修改"); }
        }

        protected bool NoAddRedirect
        {
            get
            {
                bool result = CheckAction("添加");
                if (result == false)
                    HttpContext.Current.Response.Redirect("~/noright.htm", true);

                return result;
            }
        }

        /// <summary>
        /// 重定向至无权限
        /// </summary>
        /// <param name="hasPermission">是否拥有权限:False 跳转</param>
        protected void Redirect2NoRight(bool hasPermission)
        {
            if (!hasPermission) HttpContext.Current.Response.Redirect("~/noright.htm", true);
        }

        /// <summary>
        /// 是否有模块的删除权限
        /// </summary>
        protected bool IsDelete
        {
            get { return CheckAction("删除"); }
        }


        protected string ClientIP
        {
            get
            {
                /*
                string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                return result;*/

                string ip = "";
                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) // 服务器， using proxy
                {
                    //得到真实的客户端地址
                    ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
                }
                else//如果没有使用代理服务器或者得不到客户端的ip not using proxy or can't get the Client IP
                {

                    //得到服务端的地址
                    ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
                }
                return ip;
            }
        }


        /// <summary>
        /// 判断是否有某个操作的权限
        /// </summary>
        /// <param name="actionName">操作名称</param>
        /// <returns>返回true或false</returns>
        public bool CheckAction(string actionName)
        {
            bllPermission permission = new bllPermission();
            DataTable dt = permission.GetUserRolePermission();
            if (dt == null)
            {
                return false;
            }
            DataRow[] dr = dt.Select(string.Format(" ROLE_KEY='{0}' and MODULE_NAME_EN='{1}' and ACTION_NAME='{2}'", bllSYS_USER.CurrentUser.ROLE_KEY.ToString(), MODULE_NAME_EN, actionName));
            return dr.Length > 0;
        }

        #region IsShowOfButton
        /// <summary>
        /// View Permission
        /// </summary>
        public string DispView { get { return IsView ? "" : "none"; } }
        /// <summary>
        /// Add Permission
        /// </summary>
        public string DispAdd { get { return IsAdd ? "" : "none"; } }
        public string DispEdit { get { return IsEdit ? "" : "none"; } }
        public string DispDelete { get { return IsDelete ? "" : "none"; } }
        #endregion
    }
}